package com.project.erp.service.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.project.erp.common.utils.Page;
import com.project.erp.dao.system.ParamDao;
import com.project.erp.dao.system.ParamValueDao;
import com.project.erp.model.system.Param;
import com.project.erp.model.system.ParamValue;
import com.project.erp.service.system.ParamService;

@Service
@Transactional
public class ParamServiceImpl implements ParamService {
	
	@Autowired
	private ParamDao paramDao;
	@Autowired
	private ParamValueDao paramValueDao;
	//@Autowired
	//private ParamValueDao paramValueDao;
	
	//只读事务，可以提高效率
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public Page<Param> getParamPageBySearch(Page<Param> page, Param param) {
		return paramDao.getParamPageBySearch(page, param);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void saveParam(Param param) {
		paramDao.save(param);
	}

	//只读事务，可以提高效率
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public Param getParamById(Long paramId) {
		return paramDao.get(paramId);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void editParam(Param param,List<ParamValue> delParaValueList,Long[] paramValueIds) {
		//检查是否有删除的记录
		List<Long> delParamValueIds = new ArrayList<Long>();
		boolean delflag;//是否删除记录
		
		if(paramValueIds != null){//针对细表记录为空情况
			for(ParamValue para:delParaValueList){
				delflag = true;
				String paramValueId1 = "";
				String paramValueId2 = "";
				for(int i=0;i<paramValueIds.length;i++){
					
					if(paramValueIds[i] != null){
						paramValueId1 = String.valueOf(paramValueIds[i]);
						paramValueId2 = String.valueOf(para.getId());
						if(paramValueId1.equals(paramValueId2)){
							delflag = false;
						}
					}
				}
				if(delflag){
					//增加要删除的记录id到列表
					delParamValueIds.add(para.getId());
				}
			}
		}else{
			//删除全部细表记录
			if((delParaValueList != null) && (delParaValueList.size() > 0)){
				for(ParamValue para:delParaValueList){
					delParamValueIds.add(para.getId());
				}
			}
		}
		
		//先删除要删除的细表记录
		if((delParamValueIds != null) && (delParamValueIds.size() > 0)){
			paramValueDao.deleteByIdsList(delParamValueIds);
		}
		
		paramDao.save(param);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void delParam(String[] paramIds) {
		if(null == paramIds || paramIds.length == 0){
			return;
		}
		List<Long> ids = new ArrayList<Long>();
		for (String paramId : paramIds) {
			ids.add(Long.valueOf(paramId));
		}
		//删除细表关联数据
		//paramValueDao.delParamByparamValueIds(ids);
		//删除主表信息
		paramDao.delParamByparamIds(ids);
	}

	@Override
	public Map<String, Map<String, String>> getAllParams() {
		List<Param> params = paramDao.getAll();
		
		Map<String, Map<String, String>> paramMap = new HashMap<String, Map<String, String>>();
		for(Param param: params){
			Map<String, String> parameValueMap = new HashMap<String, String>();
			for(ParamValue paramValue: param.getParamList()){
				parameValueMap.put(paramValue.getParamKey(), paramValue.getParamValue());
			}
			paramMap.put(param.getNameEn(), parameValueMap);
		}
		return paramMap;
	}

	//只读事务，可以提高效率
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public Param getParamByNameEn(String nameEn) {
		return paramDao.getParamByItemEn(nameEn);
	}
}
